
import random
import re
from datetime import datetime

from flask import current_app, jsonify
from flask import json
from flask import request
from flask import session

from info import constants, db
from info import redis_store
from info.libs.yuntongxun.sms import CCP
from info.models import User
from info.modules.passport import passport_blue
from info.utils.captcha.captcha import captcha



# 退出
@passport_blue.route('/logout', methods=['DELETE'])
def logout():
    pass
# def logout():

    # 清除session信息
    session.pop('user_id', None)
    session.pop('nick_name', None)
    session.pop('mobile', None)

    return jsonify(errno=RET.OK, errmsg='退出成功')



# 登入
@passport_blue.route('/login', methods=['POST'])
def login():
    # 1.获取参数
    dict_data = request.json
    mobile = dict_data.get('mobile')
    password = dict_data.get('password')

    # 校验参数
    if not all([mobile, password]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')

    # 根据手机号取出用户对象
    try:
        user = User.query.filter(mobile == User.mobile).first()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询用户失败')

    # 判断用户是否存在
    if not user:
        return jsonify(errno=RET.NODATA, errmsg='该用户不存在')

    # 判断密码的正确性
    if not user.check_passowrd(password):
        return jsonify(errno=RET.DATAERR, errmsg='密码输入有误')

    # 将用户的登入信息保存到session
    session['user_id'] = user.id
    session['nick_name'] = user.nick_name
    session['mobile'] = user.mobile

    user.last_login = datetime.now()

    # # 更新时间到数据库
    # try:
    #     db.session.commit()
    # except Exception as e:
    #     current_app.logger.error(e)

    # 返回响应对象
    return jsonify(errno=RET.OK, errmsg='登入成功!')



# 注册
@passport_blue.route('/register', methods=['POST'])
def register():
    # 1.获取参数
    dict_data = request.json
    mobile = dict_data.get('mobile')
    sms_code = dict_data.get('sms_code')
    password = dict_data.get('password')

    # 2.检验参数,为空校验
    if not all([mobile, sms_code, password]):
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')

    # 3.手机号格式验证
    if not re.match('1[345678]\\d{9}', mobile):
        return jsonify(errno=RET.DATAERR, errmsg='手机号格式有误')

    # 4.根据手机号取出redis中的短信验证码
    try:
        redis_sms_code = redis_store.get('sms_code:%s' % mobile)
        print(redis_sms_code)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='获取短信验证码失败')

    # 5. 判断短信验证码是否过期
    if not redis_sms_code:
        return jsonify(errno=RET.NODATA, errmsg='短信验证码已过期')

    # 6.校验短信验证码是否正确
    if sms_code != redis_sms_code:
        return jsonify(errno=RET.DATAERR, errmsg='短信验证码填错误')

    # 7.创建用户对象
    user = User()

    # 8.设置用户对象的属性信息
    user.nick_name = mobile
    user.password = password
    user.mobile = mobile

    # 9.保存用户到数据库
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        db.session.rollback()
        return jsonify(errno=RET.OK, errmsg='注册成功')








"""

1.接收参数
2.校验参数
3.校验手机号格式是否正确
4.通过验证码编号取出图片验证码
5.判断验证A是否过期
6.判断图片验证码是否正确
7.调用ccp发送短信方法
8.判断短信是否发送成功
9.保存短信到redis中
10.返回发送的状态
"""""


# 发送短信验证码#
@passport_blue.route('/sms_code', methods=['POST'])
def send_message():
    # 1.接收参数
    json_data = request.data
    dict_data = json.loads(json_data)
    mobile = dict_data.get('mobile')
    image_code = dict_data.get('image_code')
    image_code_id = dict_data.get('image_code_id')
    print(mobile)
    print(image_code)
    print(image_code_id)
    print(2)
    # 2.校验参数
    if not all([mobile, image_code, image_code_id]):
        print("333")
        return jsonify(errno=RET.PARAMERR, errmsg='参数不完整')
    print(3)
    # 3.验证手机号格式是否正确
    if not re.match('1[345678]\\d{9}', mobile):
        print("444")
        return jsonify(error=RET.DATAERR, errmsg='手机号格式有误')
    print(4)
    # 4.通过验证码编号,取出图片验证码A
    try:
        redis_image_code = redis_store.get('image_code:%s' % image_code_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error=RET.DBERR, errmsg='redis获取异常')
    print(5)
    # 5.判断图片验证码是否过期
    if not redis_image_code:
        print("111")

        return jsonify(error=RET.NODATA, errmsg='图片验证码过期')
    print(6)
    # 6.判断验证码是否正确
    if redis_image_code.upper() != image_code.upper():
        print("222")
        return jsonify(error=RET.DATAERR, errmsg='图片填写错误')
    print(7)
    # 7.调用cpp发送短信方法
    # 生成随机的六位数短信验证码
    num = random.randint(0, 999999)
    sms_code = '%06d' % num
    print(sms_code)
    # try:
    #     ccp = CCP()
    #     result = ccp.send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES/60], 1)
    # except Exception as e:
    #     current_app.logger.error(e)
    #     return jsonify(error=RET.THIRDERR, errmsg='云通讯发送异常')
    # print(8)
    # # 8.判断短信是否发送成功
    # if result == -1:
    #     return jsonify(error=RET.DATAERR, errmsg='短信发送失败')
    # print(9)
    # 9.保存短信到redis中
    try:
        redis_store.set('sms_code:%s' % mobile, sms_code, constants.SMS_CODE_REDIS_EXPIRES)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(error=RET.DBERR, errmsg='短信验证码保存异常')
    print("ok")
    # 10.返回发送的状态
    return jsonify(error=RET.OK, errmsg='短信发送成功')



# 生成图片验证码
# 请求路径:/passport/image_code
# 请求方式:GET
# 请求参数:随机字符串
# 返回值:图片验证码
from info.utils.response_code import RET

# 生成图片验证码
@passport_blue.route('/image_code')
def get_image_code():

    # 生成图片验证码
    # from info.utils.captcha.captcha import captcha
    name, text, image_data = captcha.generate_captcha()

    # 获取参数
    cur_id = request.args.get('cur_id')
    pre_id = request.args.get('pre_id')

    try:
        # 保存图片验证码到redis
        redis_store.set('image_code:%s' % cur_id, text, constants.IMAGE_CODE_REDIS_EXPIRES)

        # 判断是否有上一个图片验证编号
        if pre_id:
            redis_store.delete('image_code:%s' % pre_id)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='redis操作异常')

    # 返回图片验证码
    return image_data